import requests
import json

app_id = "3d661451-a066-4186-bcb4-d1b57f2bc858"
auth =  'Bearer bce-v3/ALTAK-JF19wDK4Fjm8QVJkSng9F/e8e69f73fa351aeca4c3926e0bca1e15b565b53f'


url = "https://qianfan.baidubce.com/v2/app/conversation"

payload = json.dumps({
    "app_id": app_id
})
headers = {
    'Content-Type': 'application/json',
    'X-Appbuilder-Authorization': auth
}

response = requests.request("POST", url, headers=headers, data=payload)


data = json.loads(response.text)
conversation_id = data["conversation_id"]

url = "https://qianfan.baidubce.com/v2/app/conversation/file/upload"

payload = {
    'app_id': app_id,
    'conversation_id': conversation_id
}
files = [
    ('file',('R-C.jpg',open('C:\\Users\\zh415st022\\Desktop\\641.jpg','rb'),'image/jpeg'))
]
headers = {
    'X-Appbuilder-Authorization': 'Bearer bce-v3/ALTAK-JF19wDK4Fjm8QVJkSng9F/e8e69f73fa351aeca4c3926e0bca1e15b565b53f'
}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

file_id = json.loads(response.text)['id']


url = "https://qianfan.baidubce.com/v2/app/conversation/runs"

payload = json.dumps({
    "app_id": app_id,
    "query": input("请输入问题："),
    "stream": False,
    "conversation_id": conversation_id,
    "file_ids": [
        file_id
    ]
})
headers = {
    'Content-Type': 'application/json',
    'X-Appbuilder-Authorization': auth
}

response = requests.request("POST", url, headers=headers, data=payload)


data = json.loads(response.text)
answer = data["answer"]


print(answer)

